<script setup lang="ts">
import { delAttrAPI, getallAttrAPI, reqAddorUpadateAttr } from '@/apis/attr';
import { useAttrStore } from '@/stores/modules/attr';
import { Attr, AttrList, AttrValue, AttrValueResPonseData } from '@/types/attr';
import { ref, watch,nextTick, onBeforeUnmount } from 'vue';

const attrList = ref<AttrList>([])
const attrStore = useAttrStore()
const inputArr=ref<any>([])
const screen=ref(0)
//监听仓库三级分类
watch(() => attrStore.c3Id, () => {
    attrList.value = []
    if (!attrStore.c3Id) return
    getallAttrData()

})
const getallAttrData = async () => {
    const res: AttrValueResPonseData = await getallAttrAPI(attrStore.c1Id as number, attrStore.c2Id as number, attrStore.c3Id as number)
    attrList.value = res.data

}
//定义数据
const attParms = ref<Attr>({
    attrName: '',
    attrValueList: [
    ],
    categoryId: '',
    categoryLevel: 3
})
//取消
const cancal = () => {
    screen.value=0
    //清空数据
    Object.assign(attParms.value, {
        attrName: '',
        attrValueList: [
        ],
        categoryId: attrStore.c3Id,
        categoryLevel: 3
    })
}
//添加属性
const addAttr = () => {
    //清空数据
    Object.assign(attParms.value, {
        attrName: '',
        attrValueList: [
        ],
        categoryId: attrStore.c3Id,
        categoryLevel: 3
    })
    screen.value=1
}

//修改属性
const updateAtrr = (row: any) => {
    screen.value=1
    Object.assign(attParms.value, JSON.parse(JSON.stringify(row)))
}

//添加属性值
const addAttrCode = () => {
    attParms.value.attrValueList.push({
        valueName: '',
        flag: true
    })
    nextTick(()=>{
        inputArr.value[attParms.value.attrValueList.length-1].focus()
    })
}

//保存属性
const saveAttr = async () => {
    //添加属性
    await reqAddorUpadateAttr(attParms.value)
    ElMessage.success(attParms.value.id ? '修改成功' : '添加成功')
    screen.value=0
    getallAttrData()
}
//属性值表单失去焦点
const toLook = (row: AttrValue, $index: number) => {
    //非法情况
    if (row.valueName.trim() == '') {
        attParms.value.attrValueList.splice($index, 1)
        ElMessage.error('属性值不能为空')
        return
    }
    const respeat = attParms.value.attrValueList.find(item => {
        if (item != row) {
            return item.valueName === row.valueName
        }
    })
    if (respeat) {
        attParms.value.attrValueList.splice($index, 1)
        ElMessage.error('属性值不能重复')
        return
    }
    row.flag = false
}

const toEdit = (row: AttrValue,$index:number) => {
    row.flag = true
    nextTick(()=>{
        inputArr.value[$index].focus()
    })
}

//删除属性
const onConfirm=async(row:any,$index:number)=>{
    attParms.value.attrValueList.splice($index,1)
    await delAttrAPI(row.id)
    ElMessage.success('删除成功')
    getallAttrData()
}
onBeforeUnmount(()=>{
    console.log(attrStore);
    attrStore.$reset()
    
})
</script>

<template>
    <!--  头部数 -->
    <Category :screen="screen"></Category>
    <!-- 主体内容 -->
    <el-card style="margin: 10px 0px;">
        <!-- 显示主体内容页面 -->
        <template v-if="screen==0">
            <el-button type="primary" icon="Plus" :disabled="attrStore.c3Id ? false : true"
                @click="addAttr">添加平台属性</el-button>
            <!-- 表格组件 -->
            <el-table style="margin-top: 10px;" border :data="attrList">
                <el-table-column type="index" label="序号" width="80px" align="center"></el-table-column>
                <el-table-column prop="attrName" label="属性名称" width="120px"></el-table-column>
                <el-table-column prop="id" label="属性值">
                    <template #="{ row }">
                        <el-tag style="margin: 5px;" type="primary" v-for="item in row.attrValueList" :key="item.id">{{
                            item.valueName }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="id" label="操作" width="120px">
                    <template #="{ row ,$index}">
                        <el-button type="primary" size="small" icon="Edit" @click="updateAtrr(row)"></el-button>
                        <el-popconfirm :title="`你确认删除给${row.attrName}吗?`" @confirm="onConfirm(row,$index)">
                            <template #reference>
                                <el-button type="primary" icon="delete" size="small"></el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
        </template>
        <!-- 显示添加属性页面 -->
        <template v-if="screen==1">
            <!-- 头部表单组件 -->
            <el-form>
                <el-form-item label="属性名称">
                    <el-input v-model="attParms.attrName" placeholder="请输入属性名称" style="width: 180px;"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="Plus" :disabled="attParms.attrName ? false : true"
                        @click="addAttrCode">添加属性值</el-button>
                    <el-button type="primary" @click="cancal">取消</el-button>
                </el-form-item>
            </el-form>
            <!-- 中间表格组件 -->
            <el-table style="margin-top: 10px;" border :data="attParms.attrValueList">
                <el-table-column type="index" label="序号" width="80px" align="center"></el-table-column>
                <el-table-column prop="id" label="属性值名称">
                    <template #="{ row, $index }">
                        <el-input :ref="(vc:any)=>inputArr[$index]=vc" @blur="toLook(row, $index)" v-if="row.flag" v-model="row.valueName"
                            placeholder="请输入属性值名称"></el-input>
                        <div @click="toEdit(row,$index)" v-else size="small">{{ row.valueName }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="属性值操作">
                    <template #="{row,$index}">
                        <el-popconfirm :title="`你确认删除给${row.valueName}吗?`" @confirm="onConfirm(row,$index)">
                            <template #reference>
                                <el-button type="primary" icon="delete" size="small"></el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 底部按钮组件 -->
            <div style="margin: 10px 0px;">
                <el-button type="primary" :disabled="attParms.attrValueList.length > 0 ? false : true"
                    @click="saveAttr">保存</el-button>
                <el-button type="primary" @click="cancal">取消</el-button>
            </div>
        </template>
    </el-card>
</template>

<style scoped lang="scss"></style>